package com.hdd.biz.painting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdd.biz.app.mapper.APPPaintingWorksMapper;
import com.hdd.biz.member.entity.Member;
import com.hdd.biz.member.service.MemberService;
import com.hdd.biz.painting.dto.WorksListRequest;
import com.hdd.biz.painting.dto.WorksRequest;
import com.hdd.biz.painting.entity.PaintingWorks;
import com.hdd.biz.painting.entity.UserFollow;
import com.hdd.biz.painting.mapper.PaintingWorksMapper;
import com.hdd.biz.painting.service.FavourService;
import com.hdd.biz.painting.service.PaintingTopicLinkService;
import com.hdd.biz.painting.service.PaintingWorksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdd.biz.painting.service.UserFollowService;
import com.hdd.dto.SuperResponse;
import com.hdd.utils.TimeUtils;
import com.hdd.utils.wx.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 作品表 服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-02-25
 */
@Service
public class PaintingWorksServiceImpl extends ServiceImpl<PaintingWorksMapper, PaintingWorks> implements PaintingWorksService {


    @Autowired
    private UserFollowService userFollowService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private PaintingTopicLinkService topicLinkService;
    @Autowired
    private APPPaintingWorksMapper appPaintingWorksMapper;


    @Override
    public SuperResponse worksList(WorksListRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        Page<PaintingWorks> pages = new Page<>(request.getPageNo(),request.getPageSize());
        wrapper.eq("del_flag",0);
        wrapper.eq("shows",2);
        if(StringUtils.isNotEmpty(request.getIstop())){
         wrapper.eq("istop",request.getIstop());
        }
        if(StringUtils.isNotEmpty(request.getIsrecommend())){
            wrapper.eq("recommend",request.getIsrecommend());
        }
        wrapper.orderByDesc("create_time");
        IPage<PaintingWorks> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<PaintingWorks> agents = mapIPage.getRecords();
        Member member = null;
        for(PaintingWorks works:agents){
             member =  memberService.findMemberByUid(works.getUserId());
             if(member!=null){
                 works.setNickName(member.getNickname());
                 works.setHeadImgUrl(member.getHeadImgUrl());
             }
        }
        response.setItems(agents);
        response.setTotals(pages.getTotal());
        return response;
    }


    @Override
    public SuperResponse listForMobile(WorksListRequest request) {
        SuperResponse response = new SuperResponse();

        Map<String,Object> map = new HashMap<>();
        map.put("pageNo",(request.getPageNo()-1)*request.getPageSize());
        map.put("pageSize",request.getPageSize());
        if(StringUtils.isEmpty(request.getIsrecommend())){
            if("0".equals(request.getIsrecommend())){
                map.put("recommend",0);
            }
        }
        List<PaintingWorks> works = appPaintingWorksMapper.recommend(map);
        response.setItems(works);
        return response;
    }

    @Override
    public SuperResponse recommend(WorksRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("id",request.getId());

        PaintingWorks works = baseMapper.selectOne(wrapper);
        if(works!=null){
            if(works.getRecommend()==0){
                redisUtil.set("update:"+works.getUserId(),request.getId());
                works.setRecommend(1);
            }else{
                works.setRecommend(0);
            }
            works.setShows(2);
            works.setAdopt("T");
        }
        baseMapper.updateById(works);
        return response;
    }

    @Override
    public SuperResponse setTop(WorksRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("id",request.getId());

        PaintingWorks works = baseMapper.selectOne(wrapper);
        if(works!=null){
            if("T".equals(works.getIstop())){
                works.setIstop("F");
            }else{
                works.setIstop("T");
            }
        }
        works.setModifyTime(LocalDateTime.now());
        baseMapper.updateById(works);
        return response;
    }


    @Override
    public SuperResponse reject(WorksRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("id",request.getId());
        PaintingWorks works = baseMapper.selectOne(wrapper);
        if(works!=null){
            works.setAdopt("F");
            works.setShows(1);
            works.setRecommend(0);
        }
        works.setModifyTime(LocalDateTime.now());
        baseMapper.updateById(works);
        return response;
    }

    @Override
    public SuperResponse worksInfo(WorksRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("id",request.getId());
        PaintingWorks works = baseMapper.selectOne(wrapper);
        if(works!=null){
            Member  member =  memberService.findMemberByUid(works.getUserId());
            if(member!=null){
                works.setNickName(member.getNickname());
                works.setHeadImgUrl(member.getHeadImgUrl());
            }
        }

        response.setItem(works);
        return response;
    }


    @Override
    public SuperResponse delete(WorksRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("id",request.getId());
        PaintingWorks works = baseMapper.selectOne(wrapper);
        works.setDelFlag(1);
        works.setModifyTime(LocalDateTime.now());
        baseMapper.updateById(works);
        return response;
    }


    @Override
    public PaintingWorks findWorksById(String id) {
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("id",id);
        return  baseMapper.selectOne(wrapper);
    }

    @Override
    public SuperResponse preview(WorksRequest request) {
        SuperResponse response = new SuperResponse();


        return response;
    }


    @Override
    public SuperResponse adopt(WorksRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("id",request.getId());
        PaintingWorks paintingWorks = new PaintingWorks();
        paintingWorks.setId(request.getId());
        paintingWorks.setShows(2);
        paintingWorks.setAdopt("T");
        baseMapper.update(paintingWorks,wrapper);
        PaintingWorks works =  baseMapper.selectOne(wrapper);
        if(works!=null){ //发布跟新推送
            redisUtil.set("update:"+works.getUserId(),request.getId());
            List<UserFollow> userIds = userFollowService.userIds(works.getUserId());
            for(UserFollow u:userIds){
                redisUtil.set("update:"+u.getUserId(),request.getId());
            }
        }
        return response;
    }


    @Override
    public SuperResponse uploadAudio(WorksRequest request) {
        SuperResponse response = new SuperResponse();
        PaintingWorks paintingWorks = new PaintingWorks();
        paintingWorks.setMediaUrl(request.getAudiourl());
        paintingWorks.setMediaTime(request.getMediaTime());
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("id",request.getId());
        baseMapper.update(paintingWorks,wrapper);
        return response;
    }

    @Override
    public SuperResponse complaintList(WorksListRequest request) {
        return null;
    }


    @Override
    public SuperResponse recoveryList(WorksListRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        Page<PaintingWorks> pages = new Page<>(request.getPageNo(),request.getPageSize());
        wrapper.eq("del_flag","1");
        wrapper.orderByDesc("modify_time");
        IPage<PaintingWorks> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<PaintingWorks> agents = mapIPage.getRecords();
        Member member = null;
        for(PaintingWorks works:agents){
            member =  memberService.findMemberByUid(works.getUserId());
            if(member!=null){
                works.setNickName(member.getNickname());
                works.setHeadImgUrl(member.getHeadImgUrl());
            }
        }
        response.setItems(agents);
        response.setTotals(pages.getTotal());
        return response;
    }


    @Override
    public SuperResponse trialList(WorksListRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        Page<PaintingWorks> pages = new Page<>(request.getPageNo(),request.getPageSize());
        wrapper.eq("del_flag",0);
        wrapper.eq("shows",1);
        wrapper.eq("recommend",0);
        wrapper.isNull("adopt").or().eq("adopt","R");
        wrapper.orderByDesc("create_time");
        IPage<PaintingWorks> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<PaintingWorks> agents = mapIPage.getRecords();
        Member member = null;
        for(PaintingWorks works:agents){
            member =  memberService.findMemberByUid(works.getUserId());
            if(member!=null){
                works.setNickName(member.getNickname());
                works.setHeadImgUrl(member.getHeadImgUrl());
            }
        }
        response.setItems(agents);
        response.setTotals(pages.getTotal());
        return response;
    }


    @Override
    public SuperResponse rejectList(WorksListRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        Page<PaintingWorks> pages = new Page<>(request.getPageNo(),request.getPageSize());
        wrapper.eq("del_flag",0);
        wrapper.eq("adopt","F");
        wrapper.orderByDesc("create_time");
        IPage<PaintingWorks> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<PaintingWorks> agents = mapIPage.getRecords();
        Member member = null;
        for(PaintingWorks works:agents){
            member =  memberService.findMemberByUid(works.getUserId());
            if(member!=null){
                works.setNickName(member.getNickname());
                works.setHeadImgUrl(member.getHeadImgUrl());
            }
        }
        response.setItems(agents);
        response.setTotals(pages.getTotal());
        return response;
    }

    @Override
    public SuperResponse recovery(WorksRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("id",request.getId());
        PaintingWorks works = baseMapper.selectOne(wrapper);
        works.setDelFlag(0);
        works.setModifyTime(LocalDateTime.now());
        baseMapper.updateById(works);
        return response;
    }

    @Override
    public SuperResponse recoveryDel(WorksRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("id",request.getId());
        baseMapper.delete(wrapper);
        return response;
    }

    @Override
    public int getDrafts(String userId) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        wrapper.eq("shows",0);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public int views(String UserId) {
        return baseMapper.views(UserId);
    }

    @Override
    public int beLikeds(String userId) {
        return baseMapper.beLikeds(userId);
    }



    @Override
    public int works(String userId) {
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        wrapper.eq("del_flag",0);
        wrapper.ge("shows",1);
        wrapper.eq("wiki_id",1);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public int wikis(String userId) {
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        wrapper.eq("del_flag",0);
        wrapper.ge("shows",0);
        wrapper.gt("wiki_id",1);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public int totalWorks() {
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public int todayWorksWorks() {
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.last("WHERE TO_DAYS(create_time) = TO_DAYS(NOW())");
        return baseMapper.selectCount(wrapper);
    }


    @Override
    public SuperResponse topickLink() {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.isNotNull("topics");
        List<PaintingWorks> list =  baseMapper.selectList(wrapper);
        String ids[];
        for(int i=0;i<list.size();i++){
            if(StringUtils.isNotEmpty(list.get(i).getTopics())){
                ids = list.get(i).getTopics().split(",");
                for(int x=0;x<ids.length;x++){
                    topicLinkService.insert(list.get(i).getId(),ids[x]);
                }
            }
        }
        return response;
    }

    @Override
    public SuperResponse topickLink2() {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.in("id",topicLinkService.getWorksId("1"));
        List<PaintingWorks> list =  baseMapper.selectList(wrapper);
        response.setItems(list);
        return response;
    }


    @Override
    public SuperResponse setAuthor(WorksRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.eq("id",request.getId());
        PaintingWorks paintingWorks = new PaintingWorks();
        paintingWorks.setUserId(request.getUserId());
        baseMapper.update(paintingWorks,wrapper);
        return response;
    }


    @Override
    public int yesterdayWorks() {
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.last("WHERE TO_DAYS(NOW( )) - TO_DAYS(create_time) = 1");
        return baseMapper.selectCount(wrapper);
    }


    @Override
    public int weekWorks() {
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.last("WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= create_time");
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public int monthWorks() {
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper();
        wrapper.last("WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= create_time");
        return baseMapper.selectCount(wrapper);
    }
}
